<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="违法、违规信息" prop="violation" label-width="110px">-->
      <!--        <el-select v-model="queryParams.violation" placeholder="请选择违法、违规信息" clearable size="small">-->
      <!--          <el-option-->
      <!--            v-for="dict in violationOptions"-->
      <!--            :key="dict.dictValue"-->
      <!--            :label="dict.dictLabel"-->
      <!--            :value="dict.dictValue"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="违法、违规信息" prop="violation" label-width="110px">
        <treeselect v-model="queryParams.violation" :options="violationOptions" :show-count="true"
                    placeholder="请选择违法、违规信息" style="width:300px;"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['frontierEvent:event:violationStatisticsExport']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="shipList" row-key="violation"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}">">
      <el-table-column label="违法、违规信息分类" align="left" header-align="center" prop="violationName"/>
      <el-table-column label="案件数量" align="center" prop="violationNum"/>
    </el-table>

    <!--    <pagination-->
    <!--      v-show="total>0"-->
    <!--      :total="total"-->
    <!--      :page.sync="queryParams.pageNum"-->
    <!--      :limit.sync="queryParams.pageSize"-->
    <!--      @pagination="getList"-->
    <!--    />-->
    <div id="myChart"></div>

  </div>
</template>

<script>
  import {listViolationStatistics, violationStatisticsExport} from "@/api/frontierEvent/event";
  import echarts from "echarts";
  import {treeselect} from "@/api/basicInformation/violationType";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "violationStatistics",
    components: {
      Treeselect,
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 船舶信息表格数据
        shipList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 案件来源字典
        violationOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          violation: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getTreeselect();
      // this.getDicts("evt_violation_type").then(response => {
      //   this.violationOptions = response.data;
      // });
    },
    methods: {
      getTreeselect() {
        treeselect().then(response => {
          this.violationOptions = response.data;
        });
      },
      /** 查询船舶信息列表 */
      getList() {
        this.loading = true;
        listViolationStatistics(this.queryParams).then(response => {
          this.shipList = response.rows;
          // this.total = response.total;
          this.loading = false;

          let nameList = [];
          let dataList = [];
          for (var i = 0; i < response.rows.length; i++) {
            nameList.push(response.rows[i].violationName);
            dataList.push(response.rows[i].violationNum);
          }

          // 实例化echarts对象
          let myChartDrawer = echarts.init(document.getElementById('myChart'))

          // 绘制条形图
          var option = {
            backgroundColor: '#0C2E59',
            color: ['#91c7ae'],
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'line'
              }
            },
            grid: {
              bottom: '30%'
            },
            title: {
              text: '违法、违规信息分类统计',
              top: 5,
              left: 'center',
              textStyle: {
                color: '#F5F6F8'
              }
            },
            legend: {
              data: ['案件数量'],
              top: 30,
              textStyle: {
                color: '#F5F6F8'
              }
            },
            // X轴
            xAxis: {
              data: nameList,
              axisLabel: {
                interval: 0,
                rotate: 40
              },
              axisLine: {
                lineStyle: {
                  color: '#F5F6F8'
                }
              }
            },
            // Y轴
            yAxis: {
              axisLine: {
                lineStyle: {
                  color: '#F5F6F8'
                }
              }
            },
            // 数据
            series: [
              {
                name: '案件数量',
                type: 'bar',
                data: dataList,
                barWidth: 30,
                itemStyle: {
                  normal: {
                    color: '#5AC4B9'
                  }
                }
              }
            ]
          };

          myChartDrawer.setOption(option);
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return violationStatisticsExport(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>

<style>
  #myChart {
    width: 70%;
    min-height: 400px;
    clear: both;
    box-sizing: border-box;
    margin: 80px auto;
  }
</style>
